package com.yy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.result.PageResult;
import com.yy.common.result.Result;
import com.yy.entity.Hospitalprice;
import com.yy.entity.Inoutpatienttype;
import com.yy.entity.Register;
import com.yy.entity.Regiterlist;
import com.yy.model.dto.His;
import com.yy.query.RegisterQuery;
import com.yy.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author Yang
 * @Create 2024/6/13 9:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accounting")
@Tag(name = "住院管理.项目记账")
public class AccountingController {

    //入院
    private final ReportService reportService;
    //住院表
    private final RegisterService registerService;
    //科室
    private final DepartmentsService departmentsService;
    //医生
    private final DoctorService doctorService;
    //床位
    private final BedService bedService;
    //预交表
    private final PayService payService;
    //药房表
    private final PharmacyService pharmacyService;
    //住院收费表
    private final HospitalpriceService hospitalpriceService;
    //住院项目收费表
    private final InoutpatienttypeService inoutpatienttypeService;



    @GetMapping("/listInout")
    @Operation(summary = "检查项目列表分页查询")
    public PageResult<Inoutpatienttype> listInout(RegisterQuery registerQuery) {
        IPage<Inoutpatienttype> page = inoutpatienttypeService.selectiout(registerQuery);
        return PageResult.success(page);
    }

    @GetMapping("/listinout/{inoutpatientid}")
    @Operation(summary = "根据检查项目id查询项目信息")
    public Result listinout(@PathVariable int inoutpatientid) {
        LambdaQueryWrapper<Inoutpatienttype> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Inoutpatienttype::getInoutpatientid, inoutpatientid);
        Inoutpatienttype one = inoutpatienttypeService.getOne(wrapper);
        return Result.success(one);
    }

    @GetMapping("/listregister/{register}")
    @Operation(summary = "根据住院号查询信息")
    public Result listregister(@PathVariable int register) {
        Regiterlist regiterlists = registerService.listRegister(register);
        return Result.success(regiterlists);
    }

    @GetMapping("/listinoutexamine")
    @Operation(summary = "根据住院号查询选择检查项目信息")
    public PageResult<Hospitalprice> listinoutexamine(His his) {
        QueryWrapper<Hospitalprice> wrapper = new QueryWrapper<>();
        wrapper.eq("registerid", his.getHospitalizationnumber());
        wrapper.eq("state", 1);
        Page<Hospitalprice> page = new Page<Hospitalprice>(his.getPageNum(), his.getPageSize());
        return PageResult.success(hospitalpriceService.page(page, wrapper));
    }

    @PutMapping("/updateinoutprice/{Hospitalizationnumber}/{price}")
    @Operation(summary = "选择检查项目时改变已用的金额")
    public Result updateinoutprice(
            @PathVariable Integer Hospitalizationnumber,
            @PathVariable Integer price) {
        boolean f = registerService.update(new LambdaUpdateWrapper<Register>()
                .eq(Register::getRegisterid, Hospitalizationnumber)
                .set(Register::getPrice, price));
        return Result.success(f);
    }

    @PostMapping("/addhospinout/{registerid}")
    @Operation(summary = "添加病人检查")
    public Result addhospinout(@RequestBody Inoutpatienttype inoutpatienttype,
                               @PathVariable int registerid) {
        Hospitalprice hospitalprice = new Hospitalprice();
        System.out.println("要加的数据::" + inoutpatienttype);
        BeanUtils.copyProperties(inoutpatienttype, hospitalprice);
        hospitalprice.setRegisterid(registerid);
        hospitalprice.setDurgname(inoutpatienttype.getProjectname());
        hospitalprice.setDurgnum(1);
        hospitalprice.setRepiceprice(inoutpatienttype.getPrice());
        hospitalprice.setRepicetotal(inoutpatienttype.getPrice());
        hospitalprice.setHtime(new Date());
        hospitalprice.setState(1);
        boolean save = hospitalpriceService.save(hospitalprice);
        return Result.judge(save);
    }
}
